package cn.itcast.bos.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.StandardService;
import cn.itcast.bos.web.action.common.BaseAction;

@ParentPackage("json-default")
@Namespace("/")
@Actions
@Controller
@Scope("prototype")
public class StandardAction extends BaseAction<Standard>{
	
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private StandardService standardService;
	
	
	@Action(value="standard_save",results={@Result(name="success",type="redirect",location="./pages/base/standard.html")})
	public String save(){
		standardService.save(model);
		return SUCCESS;
	}
	
	@Action(value="standard_validate",results={@Result(name="success",type="json")})
	public String validateName(){
		//获取页面传递的值
		String name = model.getName();
		
		List<Standard> list = standardService.findByName(name);
		//数据库中不存在记录(对)
		if(list!=null&&list.size()==0){
			ServletActionContext.getContext().getValueStack().push(true);
		}
		else{
			ServletActionContext.getContext().getValueStack().push(false);
		}
		return SUCCESS;
	}
	
	//无条件的分页查询(查询所有)
	@Action(value="standard_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery(){
		//组装分页的条件
		//PageRequest对象来说,参数一表示当前页,从0开始(0表示第一页)
		Pageable pageable = new PageRequest(page-1, rows);
		Page<Standard> page = standardService.findPageQuery(pageable);
		//返回json数据
		/*
		 * {
				"total": 100,
				"rows" : [
					{"id":1,"name":"冰箱","price":1000},
					{"id":2,"name":"电视","price":2000},
					{"id":3,"name":"笔记本","price":4000}
				]
			}
		 */
		this.pushPageDataToValueStack(page);
		return SUCCESS;
	}
	
	//传递ids
	private String ids;
	
	public void setIds(String ids) {
		this.ids = ids;
	}
	@Action(value="standard_delete",results={@Result(name="success",type="redirect",location="./pages/base/standard.html")})
	public String delete(){
		String[] arrayids = ids.split(",");
		standardService.deleteStandardByIds(arrayids);
		return SUCCESS;
	}
	
	//查询所有
	@Action(value="standard_findAll",results={@Result(name="success",type="json")})
	public String findAll(){
		List<Standard> list = standardService.findAll();
		ServletActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
	

}
